package com.rybbaby.tss.front.tssMealFee.service.imp;

import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssMealFee.dao.ITssMealFeeDao;
import com.rybbaby.tss.front.tssMealFee.entity.TssMealFee;
import com.rybbaby.tss.front.tssMealFee.service.ITssMealFeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description 订金表数据库操作服务实现
 * 
 * @author 雷春琼
 * 
 * @createDate 2017-08-09 下午11:56:55
 */
@Service
@Transactional
public class TssMealFeeServiceImp extends BaseServiceImp<TssMealFee> implements ITssMealFeeService {

    @Autowired
    private ITssMealFeeDao tssMealFeeDao;

    @Override
    public TssMealFee findByStuId(String stuId) throws Exception {
        return tssMealFeeDao.findByStuId(stuId);
    }

    @Override
    public void updateTssMealFee(TssMealFee tssMealFee) {
        tssMealFeeDao.updateEntitiy(tssMealFee);
    }

    @Override
    public Serializable saveTssMealFee(TssMealFee tssMealFee) {
        return tssMealFeeDao.save(tssMealFee);
    }

    @Override
    public boolean deductionDayMealFee(TssMealFee tssMealFee) {
        boolean b = false;
        Double dayMealFee = tssMealFee.getDayMealFee();
        Double amount = tssMealFee.getAmount();
        Double consumptionAmount = tssMealFee.getConsumptionAmount();
        tssMealFee.setAmount(amount-dayMealFee);
        tssMealFee.setConsumptionAmount(consumptionAmount+dayMealFee);
        int i = tssMealFeeDao.deductionDayMealFee(tssMealFee);
        if(i>0){
            b = true;
        }
        return b;
    }

    @Override
    public boolean plusDayMealFee(TssMealFee tssMealFee) {
        boolean b = false;
        Double dayMealFee = tssMealFee.getDayMealFee();
        Double amount = tssMealFee.getAmount();
        Double consumptionAmount = tssMealFee.getConsumptionAmount();
        tssMealFee.setAmount(amount+dayMealFee);
        tssMealFee.setConsumptionAmount(consumptionAmount-dayMealFee);
        int i = tssMealFeeDao.plusDayMealFee(tssMealFee);
        if(i>0){
            b = true;
        }
        return b;
    }

    @Override
    public boolean batchDeductionDayMealFee(List<Map<String, Object>> packageInformation) {
        ArrayList<Object[]> params = new ArrayList<>();
        for (Map<String, Object> map : packageInformation) {
            Integer isComplimentaryMeals = Integer.parseInt(map.get("isComplimentaryMeals").toString());
            Integer formalClass = Integer.parseInt(map.get("formalClass").toString());
            if(isComplimentaryMeals == 0){
                Object memberId = map.get("memberId");
                params.add(new Object[]{memberId});
            }
            if(isComplimentaryMeals == 1){
                if(formalClass > 0){
                    Object memberId = map.get("memberId");
                    params.add(new Object[]{memberId});
                }
            }
        }
        boolean i = tssMealFeeDao.batchDeductionDayMealFee(params);
        return i;
    }
}
